package com.biz.primus.ms.order.convertor;

import com.biz.primus.model.depot.vo.DepotRespVo;
import com.biz.primus.model.oms.enums.returns.ReturnState;
import com.biz.primus.model.oms.stream.ReturnCollectionChannel;
import com.biz.primus.model.oms.vo.ExpressInfoVo;
import com.biz.primus.model.oms.vo.OmsItemVo;
import com.biz.primus.model.oms.vo.UserOrderOperationRequest;
import com.biz.primus.model.oms.vo.returns.UserFillReturnExpressInfoRequestVo;
import com.biz.primus.model.oms.vo.returns.UserOperateReturnRequestVo;
import com.biz.primus.model.order.enums.ReturnType;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderRequestVO;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderResponseVO;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.OrderItem;
import com.biz.primus.ms.order.dao.po.ReturnOrder;
import com.biz.primus.ms.order.dao.po.ReturnOrderItem;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * 退货单转换器
 *
 * @author Tiger Feng
 * @date 2017年11月20日
 */
public class ReturnOrderConvertor {
    public static ReturnOrder reqVO2PO(ReturnOrderRequestVO vo, DepotRespVo depotRespVo) {
        return reqVO2POFull(vo, depotRespVo, true);
    }

    public static ReturnOrder reqVO2POSimple(ReturnOrderRequestVO vo, DepotRespVo depotRespVo) {
        return reqVO2POFull(vo, depotRespVo, false);
    }

    private static ReturnOrder reqVO2POFull(ReturnOrderRequestVO vo, DepotRespVo depotRespVo, boolean full) {
        if (vo == null) {
            return null;
        }
        ReturnOrder po = new ReturnOrder();
        BeanUtils.copyProperties(vo, po);
        if (depotRespVo != null) {
            setDepotInfo2POReturnOrderPO(depotRespVo, po);
        }
        if (full) {
            if (vo.getItems() != null) {
                po.setItems(ReturnOrderItemConvertor.reqVOs2POs(vo.getItems(), po));
            }
        }
        return po;
    }

    private static void setDepotInfo2POReturnOrderPO(DepotRespVo depotRespVo, ReturnOrder po) {
        if (depotRespVo == null || po == null) {
            return;
        }
        po.setDepotConsignee(depotRespVo.getName());
        StringBuilder sb = new StringBuilder();
        sb.append(depotRespVo.getProvinceBaiduName())
                .append(depotRespVo.getCityBaiduName())
                .append(depotRespVo.getDistrictBaiduName())
                .append(depotRespVo.getAddress());
        po.setDepotAddress(sb.toString());
        // 邮政编码
        // 联系电话

    }

    public static ReturnOrderResponseVO po2RespVO(ReturnOrder po) {
        if (po == null) {
            return null;
        }
        ReturnOrderResponseVO vo = new ReturnOrderResponseVO();
        BeanUtils.copyProperties(po, vo);
        if (po.getItems() != null && !po.getItems().isEmpty()) {
            vo.setItems(ReturnOrderItemConvertor.pos2RespVOs(po.getItems()));
        }
        return vo;
    }

    /**
     * 将po专程Oms退货单VO，用于第一次推送退货单到OMS
     * @param po
     * @return
     */
    public static ReturnCollectionChannel.ReturnData po2OmsReturnDataForFirstSend(ReturnOrder po) {
        if (po == null) {
            return null;
        }
        Order order = po.getOrder();
        ReturnCollectionChannel.ReturnData returnData = new ReturnCollectionChannel.ReturnData();
        returnData.setCode(po.getReturnCode());
        returnData.setOriginOrderCode(po.getOrderCode());
        returnData.setOrderSource(order.getClient().toString());
        returnData.setDepot(order.getDepotCode());
        returnData.setLiquidated(po.getLiquidated());
        returnData.setAmount(po.getAmount());
        returnData.setFreightAmount(po.getExpressAmout());
        returnData.setUserId(order.getUserId());
        returnData.setRemark(po.getReturnUserDescription());
        returnData.setReturnState(ReturnState.begin);
        // 退款不退货
        if(po.getReturnType() == ReturnType.REFUND_AND_NO_RETURN){
            returnData.setNeedRefund(true);
        }
        // 退款并退货
        else if(po.getReturnType() == ReturnType.REFUND_AND_SALES_RETURN){
            returnData.setNeedRefund(true);
        }
        returnData.setItems(itemPOs2OmsItemVOs(po.getItems()));
        return returnData;
    }

    private static List<OmsItemVo> itemPOs2OmsItemVOs(List<ReturnOrderItem> pos){
        if(pos == null &&  pos.isEmpty()){
            return null;
        }
        List<OmsItemVo> vos = Lists.newArrayList();
        for(ReturnOrderItem po : pos){
            vos.add(itemPO2OmsItemVO(po));
        }
        return vos;
    }

    private static OmsItemVo itemPO2OmsItemVO(ReturnOrderItem po) {
        if (po == null) {
            return null;
        }
        OrderItem item = po.getOrderItem();
        OmsItemVo vo = new OmsItemVo();
        vo.setItemCode(po.getItemCode());
        vo.setCode(item.getProductCode());
        vo.setCategory(item.getCategoryId()+"");
        vo.setBrand(item.getBrandId()+"");
        vo.setName(item.getProductName());
        vo.setQuantity(po.getQuantity());
        vo.setPrice(item.getMemberPrice());
        vo.setFinalPrice(item.getFinalPrice());
        vo.setMarketPrice(item.getMarketPrice());
        vo.setDiscountAmount(item.getDiscountCouponMoney() + item.getDiscountPromotionMoney());
        vo.setDiscountPop(item.getDiscountPromotionMoney());
        vo.setDiscountCoupon(item.getDiscountCouponMoney());
        vo.setWeight(item.getWeight());

        return vo;
    }

    /**
     * 退货单物流信息转OMS退货单物流接口参数,用户补充退货单物流信息时需要
     * @param po
     * @return
     */
    public static UserFillReturnExpressInfoRequestVo po2OmsReturnExpressDataVO(ReturnOrder po){
        if (po == null) {
            return null;
        }
        UserFillReturnExpressInfoRequestVo vo = new UserFillReturnExpressInfoRequestVo();
        vo.setExpressInfo(getOmsReturnExpressInfoFromReturnOrder(po));
        vo.setUserReturnRemark(po.getExpressUserDescription());
        vo.setPictures(po.getExpressUserImages());
        vo.setReturnCode(po.getReturnCode());
        return vo;
    }

    private static ExpressInfoVo getOmsReturnExpressInfoFromReturnOrder(ReturnOrder returnOrder){
        if(returnOrder == null){
            return null;
        }
        ExpressInfoVo vo = new ExpressInfoVo();
        vo.setExpressNumber(returnOrder.getExpressNumber());
        vo.setExpressProvider(returnOrder.getExpressName());
        return vo;
    }

    /**
     * 获取OMS退货单取消视图
     * @param po
     * @return
     */
    public static UserOperateReturnRequestVo getOmsReturnOrderCancelVOFromPO(ReturnOrder po){
        if(po == null){
            return null;
        }
        UserOperateReturnRequestVo vo = new UserOperateReturnRequestVo();
        vo.setReturnCode(po.getReturnCode());
        vo.setUserId(po.getUserId());
        return vo;
    }

}
